//
//  TextViewController.m
//  深呼吸
//
//  Created by rimi on 15/12/23.
//  Copyright © 2015年 RIMI. All rights reserved.
//

#import "TextViewController.h"
#import "QuestionCardView.h"
#import "ResultOfACTViewController.h"

#define FIRST_TITLE @"在过去的4周内，在工作、学习或家中、有多少时候哮喘妨碍您进行日常活动"
#define SECOND_TITLE @"在过去4周内，您有多少次呼吸困难"
#define THIRD_TITLE @"在过去的四周内，因为哮喘症状（喘息、咳嗽、呼吸困难、胸闷或疼痛），您有多少次在夜间醒来或者早上比平时早醒"
#define FOURTH_TITLE @"在过去的四周内，您有多少次使用急救药物治疗（如沙丁胺醇）？"
#define FIFTH_TITLE @"您如何评价过去4周内您的哮喘控制情况"
@interface TextViewController()


{
    QuestionCardView *questionCard;
}


//当前第几题

@property(nonatomic,strong)UIImageView *headerView;

@property(nonatomic,strong)UIButton *finishButton;
- (void)initializeAppearance;
@end

@implementation TextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.9156 green:0.9156 blue:0.9156 alpha:1.0];
    self.view.clipsToBounds = YES;
    
    [self.view addSubview:self.headerView];
    
    
    [self initializeAppearance];
}

- (void)initializeAppearance {
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 64)];
    titleLabel.text = @"ACT评估";
    titleLabel.font = [UIFont systemFontOfSize:24];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:titleLabel];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(10, 10, 70, 50);
    [backButton setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
     [self.view addSubview:self.finishButton];
    CGRect bounds = CGRectMake(0, 0, 250, 440);
    CGPoint center = CGPointMake(375.f/2, -100);
    
    NSArray *titleColors = @[[UIColor orangeColor],[UIColor colorWithRed:0.0004 green:0.7507 blue:0.0006 alpha:1.0],[UIColor colorWithRed:0.0003 green:0.529 blue:0.0005 alpha:1.0],[UIColor colorWithRed:0.0006 green:0.0009 blue:1.0 alpha:1.0],[UIColor colorWithRed:0.4772 green:0.0003 blue:0.477 alpha:1.0]];
    NSArray *dataSource  = @[@[@"所有时间",@"大多数时间",@"有些时候",@"很少时候",@"没有"],
                             @[@"每天不止一次",@"每天一次",@"每周3至6次",@"每周1至2次",@"完全没有"],
                             @[@"每周4晚或更多",@"每周2至3晚",@"每周1次",@"1至2次",@"没有"],
                             @[@"每天3次以上",@"每周1至2次",@"每周2至3次",@"每周1次或更少",@"没有"],
                             @[@"没有控制",@"控制很差",@"有所控制",@"控制很好",@"完全控制"]];
    NSArray *titleArray = @[FIRST_TITLE,SECOND_TITLE,THIRD_TITLE,FOURTH_TITLE,FIFTH_TITLE];
    
    self.questionCards = [NSMutableArray arrayWithCapacity:5];
    self.numberDic = [NSMutableDictionary dictionaryWithCapacity:5];
    for (int i = 0; i < 5; i ++) {
        questionCard = [[QuestionCardView alloc] initWithFrame:bounds];
        questionCard.titleBackgroundColor = titleColors[i];
        questionCard.center = center;
        questionCard.dataArray = dataSource[i];
        questionCard.title = titleArray[i];
        
        
        //锚点
        questionCard.layer.anchorPoint = CGPointMake(0.5, -294.f/440);
        questionCard.layer.transform = CATransform3DMakeRotation((M_PI/4 + M_PI/36) * i, 0, 0, -1);
        [self.view addSubview:questionCard];
        [self.questionCards addObject:questionCard];
        __weak typeof(self) weakSelf = self;
        questionCard.didSelectedCellBlock = ^(NSInteger index){
            
                [weakSelf netQuestion];
            [weakSelf.numberDic setObject:@(index + 1) forKey:[NSString stringWithFormat:@"%d",i]];
            
            NSLog(@"%@",weakSelf.numberDic);
            
            [weakSelf finshText];
          
            
            

      
                  };

    }
    UISwipeGestureRecognizer *leftSwipGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rotationViews:)];
    leftSwipGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:leftSwipGesture];
    
    UISwipeGestureRecognizer *rightSwipGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rotationViews:)];
    rightSwipGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:rightSwipGesture];
    _currentIndex = 0;
}
-(void)finshText{
    if (self.numberDic.count == 5) {
        self.finishButton.hidden = NO;
    }
    
    
}

- (void)rotationViews:(UISwipeGestureRecognizer *)gesture {
    
  

    if (gesture.direction == UISwipeGestureRecognizerDirectionRight) {
        if (self.currentIndex == 0) {
            return;
        }
        
        [self.questionCards enumerateObjectsUsingBlock:^(QuestionCardView *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            CABasicAnimation *animation = [CABasicAnimation animation];
            animation.keyPath = @"transform";
            animation.fromValue = [NSValue valueWithCATransform3D:obj.layer.transform];
            obj.layer.transform = CATransform3DRotate(obj.layer.transform, M_PI/4 + M_PI/36, 0, 0, -1);
            animation.duration = 0.8;
            [obj.layer addAnimation:animation forKey:nil];
        }];
        self.currentIndex --;
    } else {
              [self netQuestion];

    }
}

-(void)netQuestion{
    if (self.currentIndex == 4) {
       
        return;
    }
 

    
    [self.questionCards enumerateObjectsUsingBlock:^(QuestionCardView *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        CABasicAnimation *animation = [CABasicAnimation animation];
        animation.keyPath = @"transform";
        animation.fromValue = [NSValue valueWithCATransform3D:obj.layer.transform];
        obj.layer.transform = CATransform3DRotate(obj.layer.transform, M_PI/4 + M_PI/36, 0, 0, 1);
        animation.duration = 0.8;
        [obj.layer addAnimation:animation forKey:nil];
    }];
    

    self.currentIndex ++;
    
}

-(void)backButtonTaped:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}
-(UIImageView *)headerView{
    
    
    
    if (!_headerView) {
        _headerView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64, 375, 64)];
        _headerView.image = [UIImage imageNamed:@"弧线"];
        
    }
    return _headerView;
}
-(UIButton *)finishButton{
    
    if (!_finishButton) {
        _finishButton =
            
            ({
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = CGRectMake(325, 20, 35, 35);
                [button setImage:[UIImage imageNamed:@"对号按钮"] forState:UIControlStateNormal];
                [button addTarget:self action:@selector(goToResult:)    forControlEvents:UIControlEventTouchUpInside];
                button.hidden =YES;
                
                button;
            });
            
        
    }
    return _finishButton;
}
-(void)goToResult:(UIButton *)sender{
    ResultOfACTViewController *RZVC = [[ResultOfACTViewController alloc]init];
    [self.navigationController pushViewController:RZVC animated:YES];
    
}
@end
